#ifndef CLOGGERINTERFACE_H
#define CLOGGERINTERFACE_H

#include "common/commonDefFun.h"

#ifdef _WIN32
#define FILENAME (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
#else
#define FILENAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#endif

#define LOGGER_NAME "libIO_CTL__"
namespace libcvos_vfr
{
	class cLoggerInterface
	{
	protected:
		cLoggerInterface();
		virtual ~cLoggerInterface();

	public:
		void setLoggerName(const char* name);
		void getLoggerName(char* buf, int bufLen);

		void setEnableLog(bool value);
		void setDisplay(bool value);
		void setLogLevel(int value);
		void setLogPath(const char* value);

		int getLoggerID();

#ifndef USE_LOG4Z
		void writeLog(const char* logInfo);
		void logFmt(int level, const char* format, ...);
#else
		void logFmt_log4z(int level,const char* file,const int line,const char*  format, ...);
#endif
		
	public:
		static void startAll();
		static void stopAll();

		static void s_setRetentionDays(int days);
	private:
		static bool s_check_is_directory(const char* dirName);
		static bool s_createRecursionDir(const char* path);
		static void s_fixPath(const char* path);
		static unsigned int s_getFileSize(const char* fileName);		
		static void s_deleteOldLogFiles(const char* dirPath, int days);

		unsigned long long getCurrentThreadID();

	public:
#ifdef USE_LOG4Z
#define LOG_FMT_DEBUG(fmt, ...) logFmt_log4z(cLoggerInterface::log_level_debug,FILENAME,__LINE__, "%s::" fmt, __FUNCTION__, ##__VA_ARGS__);
#define LOG_FMT_INFO(fmt, ...) logFmt_log4z(cLoggerInterface::log_level_info,FILENAME,__LINE__, "%s::" fmt, __FUNCTION__, ##__VA_ARGS__);
#define LOG_FMT_WARN(fmt, ...) logFmt_log4z(cLoggerInterface::log_level_warn,FILENAME,__LINE__, "%s::" fmt, __FUNCTION__, ##__VA_ARGS__);	
#define LOG_FMT_ERR(fmt, ...) logFmt_log4z(cLoggerInterface::log_level_error,FILENAME,__LINE__, "%s::" fmt, __FUNCTION__, ##__VA_ARGS__);
#else
#define LOG_FMT_DEBUG(fmt, ...) logFmt(cLoggerInterface::log_level_debug, " [%s:%d] %s:: " fmt  ,FILENAME, __LINE__,  __FUNCTION__, ##__VA_ARGS__);
#define LOG_FMT_INFO(fmt, ...) logFmt(cLoggerInterface::log_level_info, " [%s:%d] %s:: " fmt  ,FILENAME, __LINE__,   __FUNCTION__, ##__VA_ARGS__);
#define LOG_FMT_WARN(fmt, ...) logFmt(cLoggerInterface::log_level_warn, " [%s:%d] %s:: " fmt  ,FILENAME, __LINE__,   __FUNCTION__, ##__VA_ARGS__);
#define LOG_FMT_ERR(fmt, ...) logFmt(cLoggerInterface::log_level_error, " [%s:%d] %s:: " fmt  ,FILENAME, __LINE__,   __FUNCTION__, ##__VA_ARGS__);
#endif

private:
	void Init_logfile(const char *filename);
	void Close_logfile();
	void Flush_logfile();

	protected:

		int _iLogID;
		int _iLoglevel;
		int _iLogBufSize;

		bool _bDisplay;
		bool _bEnableLog;

		char _strLoggerName[256];
		char _strLogerDir[256];
		char _strLastLogerFileName[256];

		void * _pLogFile;

		static bool _bInit;
		static int _iRetentionDays;
		long long _llLastCheckTime;

	public:
		enum enum_log_level
		{
			log_level_trace = 0,
			log_level_debug,
			log_level_info,
			log_level_warn,
			log_level_error,
			log_level_alarm,
			log_level_fatal
		};
	};

}

#endif // !cLoggerInterface_H
